import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import Home from '../views/home.vue';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import {userData} from '@/store/userData';
const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/dashboard',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/dashboard',
                meta: {
                    title: '系统首页',
                
                },
                component: () => import('@/views/system/dashboard.vue'),
            },
            {
                path: '/pages/user',
                meta: {
                    title: '用户管理',
                    permiss: 'yhgl_1',
                },
                component: () => import('@/views/pages/user/index.vue'),
            },{
                path: '/pages/equipment',
                meta: {
                    title: '装备管理',
                    permiss: 'zbgl_1',
                },
                component: () => import('@/views/pages/equipment/index.vue'),
            },
            {
                path: '/pages/role',
                meta: {
                    title: '角色管理',
                    permiss: 'jsgl_1',
                },
                component: () => import('@/views/pages/role/index.vue'),
            },
            {
                path: '/pages/icon',
                meta: {
                    title: '图标管理',
                    permiss: 'icon_1',
                },
                component: () => import('@/views/pages/icon/index.vue'),
            },
            {
                path: '/ucenter',
                meta: {
                    title: '个人中心',
                },
                component: () => import('@/views/system/ucenter.vue'),
            },
            {
                path: '/theme',
                meta: {
                    title: '主题设置',
                    noAuth: true,
                    permiss: '7',
                },
                component: () => import('@/views/system/theme.vue'),
            }

        ],
    },
    {
        path: '/login',
        meta: {
            title: '登录',
            noAuth: true,
        },
        component: () => import('@/views/system/login.vue'),
    },
    {
        path: '/register',
        meta: {
            title: '注册',
            noAuth: true,
        },
        component: () => import('@/views/system/register.vue'),
    },
    {
        path: '/reset-pwd',
        meta: {
            title: '重置密码',
            noAuth: true,
        },
        component: () => import('@/views/system/reset-pwd.vue'),
    },
    {
        path: '/test01',
        meta: {
            title: '测试页面',
            permiss: 'test_0',
        },
        component: () => import('@/views/test/test01.vue'),
    },
    {
        path: '/403',
        meta: {
            title: '没有权限',
            noAuth: true,
        },
        component: () => import('@/views/system/errors/403.vue'),
    },
    {
        path: '/404',
        meta: {
            title: '找不到页面',
            noAuth: true,
        },
        component: () => import('@/views/system/errors/404.vue'),
    },
    { path: '/:path(.*)', redirect: '/404' },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    const loginKey = sessionStorage.getItem('loginKey');
    const permiss = userData().permiss;
    if (to.meta.noAuth == true) {
        // 无需认证的页面
        next();
    } else if (!loginKey) {
        // 未登录且需要认证
        next('/login');
    } else if (typeof to.meta.permiss == 'string' && !permiss.includes(to.meta.permiss)) {
        // 权限不足
        next('/403');
    } else {
        // 通过所有检查
        next();
    }
});

router.afterEach(() => {
    NProgress.done();
});

export default router;
